<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>登录页面</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/client/css/style.css">
    <style>
        html,body{
            background: linear-gradient(180deg,#369,#fff);
            height: 100vh;
            width: 100vw;
            overflow: hidden;
        }
    </style>
</head>

<body class="vh-100 d-flex flex-column justify-content-center align-items-center bg-secondary">
    <div class="card w-25">
        <div class="card-header">
            <h3 class="card-title">欢迎登录</h3>
        </div>
        <div class="card-body">
            <form onsubmit="return false" class="was-validated">
                <div class="form-row mb-3">
                    <label for="username" class="col-2 text-justify col-form-label text-right">账号：</label>
                    <input type="text" class="form-control col-7" id="username" name="username" required
                        placeholder="请填写账号">
                    <p class="invalid-feedback col-3 col-form-label">请填写账号</p>
                </div>
                <div class="form-row mb-3">
                    <label for="password" class="col-2 text-justify col-form-label text-right">密码：</label>
                    <input type="password" class="form-control col-7" id="password" name="password" required
                        placeholder="请填写密码">
                    <p class="invalid-feedback col-3 col-form-label">请填写密码</p>
                </div>
                <div class="form-row mb-3">
                    <input id="submit" type="submit" class="btn btn-primary w-100  col-7 offset-2" value="登录">
                </div>
            </form>
        </div>
        <div class="small text-center card-footer">
            还没有账号? <a href="/register" class=" card-link"
                      style="color:deepskyblue;margin-left:10px;">点击去注册</a>
        </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.0/jquery.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js"></script>
    <script>
      $(function(){
        //点击去登录
        $("#submit").click(function (){
          let username = $("#username").val();
          let password = $("#password").val();
          if(!username || !password){
            alert("用户名或密码不能为空");
            return false;
          }
          $.ajax({
            url:"http://localhost:5000/login",
            method:"post",
            data:{
              username,password
            },
            success:function(res){
             alert(res.message);
             if(res.code === 0){
               //登录成功,跳转到首页
               location.href = 'http://localhost:5000';
             }else{
               //登录失败,请重试
               location.reload();
             }
            },
            error:function(err){
              console.log(err);
            }
          });
        });
      });
    </script>
</body>

</html>